package com.wys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wys.dao.EmployeeDao;
import com.wys.pojo.Employee;
import com.wys.result.R;
import com.wys.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Service
public class  EmployeeServiceImpl extends ServiceImpl<EmployeeDao, Employee> implements EmployeeService {
    /**
     *  后台登陆操作
     * @param employee
     * @return
     */
    @Override
    public R<Employee> login(HttpServletRequest request,Employee employee) {
        //1.对接收到的密码进行MD5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2.判断用户是否存在
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee employeeLogin = getOne(queryWrapper);

        //3.判断用户是否为空
        if(employeeLogin == null){
            return R.error("用户名不存在！");
        }

        //4.判断密码是否正确
        if(!employeeLogin.getPassword().equals(password)){
            return R.error("密码错误！");
        }

        //5.查看员工状态
        if(employeeLogin.getStatus() == 0){
            return R.error("该账号已禁用！");
        }

        //6.成功登录
        request.getSession().setAttribute("employee",employeeLogin.getId());
        return R.success(employeeLogin);
    }

    /**
     *  分页和员工查询功能
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page page(int page, int pageSize, String name) {
        //分页构造器
        Page pageL=new Page(page,pageSize);
        //条件查询构造器
        LambdaQueryWrapper<Employee> wrapper=new LambdaQueryWrapper();
        //添加查询条件
        wrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //添加排序条件
        wrapper.orderByDesc(Employee::getUpdateTime);
        //执行查询
        page(pageL,wrapper);
        //如果查询出来的数据的总页面小于当前页,重新查询
        if (page > pageL.getPages()){
            pageL = this.page((int)pageL.getPages(),pageSize,name);
        }
        return pageL;
    }

    /**
     * 更新员工信息操作
     * @param request
     * @param employee
     * @return
     */

    @Override
    public R<String> update(HttpServletRequest request, Employee employee) {
        //执行修改用户操作的ID  公共字段自动填充
        /*long idL = (long) request.getSession().getAttribute("employee");
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(idL);*/
        updateById(employee);
        return R.success("员工信息修改成功!");
    }

    /**
     *   根据ID查询员工信息
     * @param id
     * @return
     */
    @Override
    public R<Employee> getEmployeeById(Long id) {
        return R.success(getById(id));
    }

}
